# _*_ coding:utf-8 _*_
# @Time  : 2023.08.24
# @Author: zizlee
import datetime
import json
import pathlib

from .fund_database import ExDoctorDatabase
from .fund_reconciliation import FundReconciler
from .close_records import CloseStatistician
from .exchange_statistics import ExchangeStatistician
from .mix_appraise import MixAppraise, SyntheticalAppraise

from settings import FILE_STORAGE


# 诊断结果文件列表(缓存生成PDF用)
def report_temp_files(prefix, start: str = '', end: str = '', clear: bool = False):
    temp_folder = pathlib.Path(FILE_STORAGE).joinpath('sysTemp/exDoctor')
    temp_file_list = [
        temp_folder.joinpath(f'{prefix}_account_overview.json'),  # 账户数据概览
        temp_folder.joinpath(f'{prefix}_cum_net_value_cum_profit.png'),  # 累计净值与累计净利润
        temp_folder.joinpath(f'{prefix}_cum_net_value_cum_profit.txt'),  # 累计净值与累计净利润
        temp_folder.joinpath(f'{prefix}_cum_pullback.png'),  # 最大回撤
        temp_folder.joinpath(f'{prefix}_cum_pullback.txt'),  # 最大回撤
        temp_folder.joinpath(f'{prefix}_net_volatility.png'),  # 当日净值与波动率
        temp_folder.joinpath(f'{prefix}_net_volatility.txt'),  # 当日净值与波动率
        temp_folder.joinpath(f'{prefix}_period_profit_daily.png'),  # 每日盈亏
        temp_folder.joinpath(f'{prefix}_period_profit_daily.txt'),  # 每日盈亏
        temp_folder.joinpath(f'{prefix}_period_profit_week.png'),  # 每周盈亏
        temp_folder.joinpath(f'{prefix}_period_profit_week.txt'),  # 每周盈亏
        temp_folder.joinpath(f'{prefix}_period_profit_month.png'),  # 每月盈亏
        temp_folder.joinpath(f'{prefix}_period_profit_month.txt'),  # 每月盈亏
        temp_folder.joinpath(f'{prefix}_close_profit_overview.json'),  # 平仓盈利与亏损
        temp_folder.joinpath(f'{prefix}_pupil_profit_count_pie.png'),  # 多空盈亏分析
        temp_folder.joinpath(f'{prefix}_pupil_profit_amount_pie.png'),  # 多空盈亏分析
        temp_folder.joinpath(f'{prefix}_pupil_profit.json'),  # 多空盈亏分析
        temp_folder.joinpath(f'{prefix}_variety_profit_rank.png'),  # 品种盈亏分析
        temp_folder.joinpath(f'{prefix}_variety_profit_rank.txt'),  # 品种盈亏分析
        temp_folder.joinpath(f'{prefix}_variety_ljrate_rank.png'),  # 单位利交比
        temp_folder.joinpath(f'{prefix}_variety_ljrate_rank.txt'),  # 单位利交比
        temp_folder.joinpath(f'{prefix}_block_profit_month.png'),  # 板块盈亏(交易统计核算)
        temp_folder.joinpath(f'{prefix}_block_profit_month.txt'),  # 板块盈亏(交易统计核算)
        temp_folder.joinpath(f'{prefix}_block_profit.png'),  # 板块盈亏2(交易统计核算)
        temp_folder.joinpath(f'{prefix}_block_profit.txt'),  # 板块盈亏2(交易统计核算)
        temp_folder.joinpath(f'{prefix}_variety_ex_amount_struct.png'),  # 品种成交结构(交易统计核算)
        temp_folder.joinpath(f'{prefix}_variety_ex_amount_struct.txt'),  # 品种成交结构(交易统计核算)
        temp_folder.joinpath(f'{prefix}_exchange_period_pie.png'),  # 交易周期类型(平仓记录核算)
        temp_folder.joinpath(f'{prefix}_exchange_period_pie.json'),  # 交易周期类型(平仓记录核算)
        temp_folder.joinpath(f'{prefix}_exchange_period_pie.txt'),  # 交易周期类型(平仓记录核算)
        temp_folder.joinpath(f'{prefix}_evaluation_score_radar.png'),  # 交易综合评价
        temp_folder.joinpath(f'{prefix}_evaluation_score.json'),  # 交易综合评价
        temp_folder.joinpath(f'{prefix}_synthetical_appraise.json'),  # 交易综合分析
    ]
    if clear:
        # 查看上一次的请求时间
        last_query = temp_folder.joinpath(f'{prefix}.json')
        if last_query.exists():
            with open(last_query, 'r', encoding='utf8') as f:
                query_info = json.load(f)
            if query_info['start'] != start or query_info['end'] != end:
                for r in temp_file_list:
                    if r.exists() and r.is_file():
                        r.unlink()
        # 写入本次查询的时间段
        with open(last_query, 'w', encoding='utf8') as wf:
            json.dump({
                'start': start,
                'end': end,
                'query_time': datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
            }, wf, indent=2, ensure_ascii=False)
        return []
    return temp_file_list
